<?php
namespace app\common\model\account;
use think\Model;
use app\common\model\orderdata\Goods;
class AccountLink extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'account_link';
    protected $pk = 'Id';

    // 创建产生台账

    /**
     * 关联创建台账
     * @return [type] [description]
     */
    public function linkcreate()
    {
        return $this->hasone('AccountCreate','Id','AccountCreateId');
    }
    public function goodinfo()
    {
        return $this->hasone('app\common\model\orderdata\Goods','GoodId','GoodId');
    }

    /**
     * 关联自利用台账
     * @return [type] [description]
     */
    public function linkselfuse()
    {
        return $this->hasone('AccountSelfUse','Id','AccountSelfUseId');
    }

    /**
     * 关联委外利用台账
     * @return [type] [description]
     */
    public function linkentrustuse()
    {
        return $this->hasone('AccountEntrustUse','Id','AccountEntrustUseId');
    }

    /**
     * 关联入库台账
     * @return [type] [description]
     */
    public function linkinwarehouse()
    {
        return $this->hasone('AccountInWarehouse','Id','AccountInWarehouseId');
    }

    public static function createlink($params)
    {
        $filelist = ['CreateId','AcceptId','InWarehouseId','OutWarehouseId','SelfUseId','EntrustUseId','CompanyId'];
        $data = new AccountLink();
        $sb = 0;//计数
        foreach ($filelist as $fk => $fv) {
            $file = 'Account'.$fv;
            if(!empty($params[$fv])){
                $data->$file = $params[$fv];
                $sb++;
            }
        }
        if($sb<3){
            return moerr('台账关联失败：缺失关联id');
        }
        $data->CreateTime = date('Y-m-d H:i:s',time());
        $data->save();
        return $data->Id;
    }
}
